package com.myself.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootPanel;
import com.sencha.gxt.widget.core.client.Dialog;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.HorizontalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.HtmlLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.event.BeforeHideEvent;
import com.sencha.gxt.widget.core.client.event.BeforeHideEvent.BeforeHideHandler;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.form.FieldLabel;
import com.sencha.gxt.widget.core.client.form.TextField;


/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ui implements EntryPoint {

 	/**
	 * Create a remote service proxy to talk to the server-side Greeting
	 * service.
	 */
	private final GreetingServiceAsync greetingService = GWT.create(GreetingService.class);
 	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		final TextButton sendButton = new TextButton("Send");
		final TextField nameField = new TextField();
		final FieldLabel errorLabel = new FieldLabel();
		final FieldLabel toServerLabel = new FieldLabel();
		toServerLabel.setText("To Server  ");
		
		HorizontalLayoutContainer horizontalPanel = new HorizontalLayoutContainer();
		horizontalPanel.add(toServerLabel);

		horizontalPanel.add(nameField);
		horizontalPanel.add(sendButton);
		horizontalPanel.add(errorLabel);
		horizontalPanel.setPagePosition(200, 200);
		horizontalPanel.setPixelSize(600, 200);
		RootPanel.get().add(horizontalPanel);
		// Use RootPanel.get() to get the entire body element
		nameField.focus();
		nameField.selectAll();
		// Create the popup dialog box
		final VerticalLayoutContainer dialogVPanel = new VerticalLayoutContainer();
		dialogVPanel.setPixelSize(200, 200);

		final Dialog dialogBox = new Dialog();
		final HtmlLayoutContainer resultPanel  = new HtmlLayoutContainer("");
		final HtmlLayoutContainer toSendValue  = new HtmlLayoutContainer("");
		// We can set the id of a widget by accessing its Element
		dialogBox.setHeadingText("Remote Procedure Call");
		dialogVPanel.add(new HtmlLayoutContainer(
				"<b>Sending name to the server:</b>"));
		dialogVPanel.add(toSendValue);
		dialogVPanel.add(new HtmlLayoutContainer("<br><b>Server replies:</b>"));
		dialogVPanel.add(resultPanel);
		
		dialogBox.setWidget(dialogVPanel);
		dialogBox.setHideOnButtonClick(true);
		dialogBox.addBeforeHideHandler( new BeforeHideHandler() {
			public void onBeforeHide(BeforeHideEvent event) {
				sendButton.enable();
				sendButton.focus();
			}
		});
		
		sendButton.addSelectHandler(new SelectHandler() {
			public void onSelect(SelectEvent event) {
				sendNameToServer();
			}

			/**
			 * communicate with RPC service
			 */
			private void sendNameToServer() {
				/**
				 * Send the name from the nameField to the server and wait for a
				 * response.
				 */
				// First, we validate the input.
				errorLabel.clear();
				final String textToServer = nameField.getValue();
				
				// Then, we send the input to the server.
				sendButton.disable();
				nameField.setText(textToServer);
				greetingService.greetServer(textToServer,
						new AsyncCallback<String>() {
							public void onSuccess(String result) {
								// Show the RPC error message to the user
								// serverResponseLabel = new
								toSendValue.setHTML(textToServer);
								resultPanel.setHTML(result);
								dialogBox.show();
								dialogBox.center();
							}

							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								dialogBox.add(new HtmlLayoutContainer("Remote Procedure Call - Failure"));
								dialogBox.show();
								dialogBox.center();
							}
						});
			}
		});

	}

}
